<?php
/**
 * Semester Section Controller
 * Semester Section  Information   Management
 *
 * semester entry
 *
 * @version $Id: SectionController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package semester
 * @subpackage Controller
 */
class Semester_SectionController extends Zend_Controller_Action
{
    function indexAction()
    {
        if (!$this->_request->getParam('year_semester')) {
            $year = $this->view->Semester['year'];
            $semester = $this->view->Semester['semester'];
            $this->view->grade = $this->view->lowYear;
            $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        }
        else {
            $this->view->year_semester = $this->_request->getParam('year_semester');
            $temp = explode("-",$this->view->year_semester);
            $year = $temp[0];
            $semester = $temp[1];
            $this->view->grade = $this->_request->getParam('sel_grade');
        }



        if ($this->getRequest()->isPost() and $this->getRequest()->getParam('act')) {
            $sort = $this->getRequest()->getParam('sort');
            $common = $this->getRequest()->getParam('common');
            $timeStart = $this->getRequest()->getParam('time_start');
            $timeEnd = $this->getRequest()->getParam('time_end');
            $section = $this->getRequest()->getParam('section');
            foreach ($sort as $sn =>$val) {
                $semesterSection = new Semester_Model_SemesterSection;
                $semesterSection->assignIdentifier($sn);
                $semesterSection->common = $common[$sn];
                $semesterSection->time_start = $timeStart[$sn];
                $semesterSection->time_end = $timeEnd[$sn];
                $semesterSection->section = $section[$sn];
                $semesterSection->sort = $sort[$sn];
                $semesterSection->save();
            }
            // 相同設定
            if ($this->getRequest()->getParam('sameset')) {
                for($i=$this->view->lowYear;$i<=$this->view->highYear;$i++){
                    if ($i  != $this->view->grade ) {
                        foreach ($sort as $sn =>$val) {
                            $semesterSection = new Semester_Model_SemesterSection;
                            $q = $semesterSection->getTable()->createQuery('a')
                            ->where('year=?', $year)
                            ->andwhere('semester=?', $semester)
                            ->andWhere('grade=?',$i)
                            ->andWhere('sort=?', $val);
                            $res = $q->fetchOne();

                            if ($res)
                                $semesterSection->assignIdentifier($res['sn']);

                            $semesterSection->year = $year;
                            $semesterSection->semester = $semester;
                            $semesterSection->grade = $i;
                            $semesterSection->common = $common[$sn];
                            $semesterSection->time_start = $timeStart[$sn];
                            $semesterSection->time_end = $timeEnd[$sn];
                            $semesterSection->section = $section[$sn];
                            $semesterSection->sort = $sort[$sn];
                            $semesterSection->save();
                        }
                    }
                }
            }
        }
        $semesterSection = new Semester_Model_SemesterSection;
        $this->view->data = $semesterSection->fetchYearSeme($year,$semester,$this->view->grade);
    }


    function deleteAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn',0);

        // 判斷是否排課
        $q = Doctrine_Query::create()
        ->select('COUNT(*) AS cc')
        ->from('Semester_Model_SemesterTimetable')
        ->where('section_sn=?',$sn);
        $res = $q->fetchOne();
        if ($res->cc==0) {
            $semesterSection = new Semester_Model_SemesterSection;
            $semesterSection->assignIdentifier($sn);
            $semesterSection->delete();
            $this->_helper->json(true);
        }
        else
        $this->_helper->json(false);
    }

    function addAction()
    {
        $arr = array();
        $f = new Zend_Filter_StripTags();
        $yearSemester = $this->_request->getParam('year_semester');
        $temp = explode("-",$yearSemester);
        $arr['year'] = (int)$temp[0];
        $arr['semester'] = (int)$temp[1];
        $arr['grade'] = (int)$this->_request->getParam('sel_grade',0);
        $arr['sort'] = (int)$this->_request->getParam('n_sort');
        $arr['section'] = $f->filter($this->_request->getParam('n_section'));
        $arr['time_start'] =  $f->filter($this->_request->getParam('n_time_start'));
        $arr['time_end'] =  $f->filter($this->_request->getParam('n_time_end'));
        $arr['common'] = $f->filter($this->_request->getParam('n_common'));
        $semesterSection = new Semester_Model_SemesterSection;
        $semesterSection->fromArray($arr);
        $semesterSection->save();
        $this->_redirect('semester/section/index');
    }
}